/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ai;

import comm.Messenger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import search.AStarSearch;
import search.Searcher;
import world.World;

/**
 *
 * @author dheath
 */
public class Tournament implements Intelligence
{

    private int botId;
    private World world;
    private Messenger msgr;
    private TourneyState state;
    private Searcher searcher;
    private Date lastTime;
    private boolean isAggressive;
    private Date gameTime;
    private double noise;
    
    public Tournament(int botId,double noise)
    {
        this.botId = botId;
        
        this.noise = noise;
        searcher = new Searcher(new AStarSearch());

        lastTime = new Date();
        gameTime = lastTime;
        try
        {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader stdin = new BufferedReader(isr);

           

            while (true)
            {
                System.out.println("Which clay pigeon?");
                System.out.println("1 - Aggressor");
                System.out.println("2 - Defense");
                System.out.println("3 - Turret");
                System.out.println("4 - SearchFlag");

                String srch = stdin.readLine();
                int searchv = 0;
                try
                {
                    searchv = Integer.parseInt(srch);
                } catch (NumberFormatException ex)
                {
                    System.out.println("choice must be a number from 1 to 4");
                    continue;
                }
                if (searchv > 0 && searchv < 5)
                {
                    state = setInitialState(searchv);
                    if(searchv == 1)
                    {
                        isAggressive = true;
                    }
                    else
                    {
                        isAggressive = false;
                    }
                    break;
                }
            }

        //stdin.close();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    
    }

    private TourneyState setInitialState(int type)
    {
        switch (type)
        {
            case 1:
                return new InitialAttack();
            case 2:
                return new InitialAttack();
            case 3:
                return new Turret(noise);
            case 4:
                return new SearchFlag();
            default:
                return new Patrol();
        }
    }
    
    public void setWorld(World world)
    {
        this.world = world;
    }

    public void setMessenger(Messenger msgr)
    {
        this.msgr = msgr;
    }

    public void setState(TourneyState state)
    {
        this.state = state;
    }

    public void NextMove()
    {
        if(!this.world.getFriends().get(botId).getStatus().equalsIgnoreCase("dead"))
        {
            state.nextMove(this);
        }
    }

    public World getWorld()
    {
        return this.world;
    }

    public Messenger getMessenger()
    {
        return this.msgr;
    }

    public Searcher getSearcher()
    {
        return this.searcher;
    }

    public int getBotId()
    {
        return this.botId;
    }

    public Date getLastTime()
    {
        return lastTime;
    }

    public void setLastTime(Date lastTime)
    {
        this.lastTime = lastTime;
    }

    public boolean isIsAggressive()
    {
        return isAggressive;
    }

    public void setIsAggressive(boolean isAggressive)
    {
        this.isAggressive = isAggressive;
    }

    public Date getGameTime()
    {
        return gameTime;
    }

    public void setGameTime(Date gameTime)
    {
        this.gameTime = gameTime;
    }

    public double getNoise()
    {
        return noise;
    }

    public void setNoise(double noise)
    {
        this.noise = noise;
    }
}
